package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.base.fragment.adapter.NoPreloadViewPager;
import com.oscar.ehome.bean.pagebean.DaKaXiqngQingBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.dateview.DateDialog;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.spinerpopwindow.AbstractSpinerAdapter;
import com.oscar.ehome.utils.spinerpopwindow.SpinerPopWindow;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.DaKaXqAdapter;
import com.oscar.ehome.view.pageview.view.DakaXiangQingView;
import com.oscar.ehome.view.pageview.view.TeamCountNewView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/11/23
 */

public class DakaxiangqingActivity extends BasesActivity<DakaXiangQingView, PagePresenter> implements DakaXiangQingView,
        AdapterView.OnItemClickListener ,AbstractSpinerAdapter.IOnItemSelectListener, XRefreshView.XRefreshViewListener {


    @BindView(R.id.title_img_back)
    ImageView titleImgBack;

    @BindView(R.id.fengexian)
    View fengexian;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRight;

    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.liebiaomoren)
    ProgressFrameLayout liebiaomoren;
    @BindView(R.id.dakaliebiao)
    MyList dakaliebiao;
    /**
     * shangwu
     */
    @BindView(R.id.shangtext)
    TextView shangtext;
    /**
     * 下午
     */
    @BindView(R.id.xiatext)
    TextView xiatext;
    /**
     * 晚上
     */
    @BindView(R.id.wantext)
    TextView wantext;
    @BindView(R.id.viewone)
    View viewone;
    @BindView(R.id.viewtwo)
    View viewtwo;
    @BindView(R.id.viewthree)
    View viewthree;
    /**
     * 全天
     */
    @BindView(R.id.quantext)
    TextView quantext;
    @BindView(R.id.biankuang)
    LinearLayout biankuang;
    @BindView(R.id.shaixuankuang)
    LinearLayout shaixuankuang;
    @BindView(R.id.shaixuanwenzi)
    TextView shaixuanwenzi;
    @BindView(R.id.page_xrefresh)
    XRefreshView pageXrefresh;
    String token = "";
    String dates = "";
    String time = "all",isFocus="0";
    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    DaKaXqAdapter adapter;
    private List<String> nameList = new ArrayList<String>();
    SpinerPopWindow mSpinerPopWindow;
    private PagePresenter pagePresenter;
    List<DaKaXiqngQingBean.ss> dakaList = new ArrayList<>();
    public static long lastRefreshTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dakaxiangqing);
        ButterKnife.bind(this);
        initView();
        shezhicolor();
        initData();
    }

    public void initView() {
        titleRightLayput.setVisibility(View.VISIBLE);
        titleImgRight.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleImgRight.setImageResource(R.mipmap.riqida);
        token = SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getToken();
               Calendar c = Calendar.getInstance();//
        int mYear = c.get(Calendar.YEAR); // 获取当前年份
        int mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        int mDate = c.get(Calendar.DATE);
        mSpinerPopWindow = new SpinerPopWindow(this);
        mSpinerPopWindow.refreshData(nameList, 0);
        mSpinerPopWindow.setItemListener(this);
        dates = mYear + "-" + mMonth + "-" + mDate;
        titleTopbar.setText(dates);
        adapter = new DaKaXqAdapter(DakaxiangqingActivity.this, dakaList);
        dakaliebiao.setAdapter(adapter);

    }
            public  void  shezhicolor(){
                shangtext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                xiatext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                wantext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                GradientDrawable drawables = (GradientDrawable) quantext.getBackground();
                //改变drawable的背景填充色
                drawables.setColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                quantext.setTextColor(Color.parseColor("#ffffff"));
                basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getColor()));
                GradientDrawable drawable = (GradientDrawable) biankuang.getBackground();
                //改变drawable的背景填充色
                drawable.setStroke(3, Color.parseColor(SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getColor()));
                viewone.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getColor()));
                viewthree.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getColor()));
                viewtwo.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().getColor()));
                pageXrefresh.setXRefreshViewListener(this);
                // 设置是否可以下拉刷新
                pageXrefresh.setPullRefreshEnable(true);
                // 设置是否可以上拉加载
                pageXrefresh.setPullLoadEnable(false);
                //设置恒华不刷新
                pageXrefresh.setMoveForHorizontal(true);
                // 设置上次刷新的时间
                pageXrefresh.restoreLastRefreshTime(lastRefreshTime);
                //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
                pageXrefresh.setMoveHeadWhenDisablePullRefresh(true);
                // 设置时候可以自动刷新
                pageXrefresh.setAutoRefresh(false);
            }
    /**
     * 加载数据
     */
    public void initData() {
        showLoading();
        if (isNetWork()) {
            getmessage();
        } else {

        }
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    public void getmessage() {
        String s = String.valueOf(page);
        pagePresenter.dakatongjixiangqing(token,"20",s, dates, time);

    }

    @OnClick({R.id.title_layout_left, R.id.title_right_layput, R.id.shangtext, R.id.xiatext,R.id.shaixuankuang, R.id.wantext, R.id.quantext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shaixuankuang:
                showPopupMenu(shaixuankuang);
                break;
            case R.id.wantext:
                setcolor();
                Resources res = getBaseContext().getResources();
                Drawable draw = res.getDrawable(R.drawable.youbianyuanjiao);
                //设置linearLayout的背景图，将Drawable对象放进去
                wantext.setBackground(draw);
                GradientDrawable drawable = (GradientDrawable) wantext.getBackground();
                //改变drawable的背景填充色
                drawable.setColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                wantext.setTextColor(Color.parseColor("#ffffff"));
                time = "night";
                initData();
                break;
            case R.id.xiatext:
                setcolor();
                xiatext.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                xiatext.setTextColor(Color.parseColor("#ffffff"));
                time = "afternoon";
                initData();
                break;
            case R.id.shangtext:
                setcolor();
                shangtext.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                shangtext.setTextColor(Color.parseColor("#ffffff"));
                time = "morning";
                initData();
                break;
            case R.id.quantext:
                setcolor();
                Resources ress = getBaseContext().getResources();
                Drawable draws = ress.getDrawable(R.drawable.zuobianyuanjiao);
                //设置linearLayout的背景图，将Drawable对象放进去
                quantext.setBackground(draws);
                GradientDrawable drawables = (GradientDrawable) quantext.getBackground();
                //改变drawable的背景填充色
                drawables.setColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                //  quantext.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
                quantext.setTextColor(Color.parseColor("#ffffff"));
                time = "all";
                initData();
                break;
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.title_right_layput:
                getDate();
                break;
        }
    }


    public void setcolor() {
        quantext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        xiatext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        shangtext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        wantext.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
        quantext.setBackgroundResource(R.color.tras);
        xiatext.setBackgroundResource(R.color.tras);
        shangtext.setBackgroundResource(R.color.tras);
        wantext.setBackgroundResource(R.color.tras);

    }

    @Override
    public void getXiangQingMessage(DaKaXiqngQingBean result) {
        pageXrefresh.stopRefresh();
        dismissLoading();
        if(isFocus.equals("0")){
            shaixuanwenzi.setText("全部人员("+result.getPunchcardNum()+"/"+result.getAllPunchcardNum()+")");
            nameList.clear();
            nameList.add("全部人员("+result.getPunchcardNum()+"/"+result.getAllPunchcardNum()+")");
            nameList.add("考勤组("+result.getFocusPunchcardNum()+"/"+result.getFocusAllPunchcardNum()+")");
            if (result.getList().size() > 0) {

                dakaliebiao.setVisibility(View.VISIBLE);
                liebiaomoren.stopRefresh();
                fengexian.setVisibility(View.VISIBLE);
                dakaList = result.getList();
                adapter.setLists(dakaList);
                if (result.getList().size() < page_size ) {
                    pageXrefresh.stopLoadMore();
                }
            } else {
                fengexian.setVisibility(View.GONE);
                dakaliebiao.setVisibility(View.GONE);
                liebiaomoren.setNoResult();
            }
        }else if (isFocus.equals("1")){
            nameList.clear();
            nameList.add("全部人员("+result.getPunchcardNum()+"/"+result.getAllPunchcardNum()+")");
            nameList.add("考勤组("+result.getFocusPunchcardNum()+"/"+result.getFocusAllPunchcardNum()+")");
            if (result.getFocusList().size() > 0) {

                dakaliebiao.setVisibility(View.VISIBLE);
                liebiaomoren.stopRefresh();
                fengexian.setVisibility(View.VISIBLE);
                dakaList = result.getFocusList();
                adapter.setLists(dakaList);
                if (result.getList().size() < page_size ) {
                    pageXrefresh.stopLoadMore();
                }
            } else {
                fengexian.setVisibility(View.GONE);
                dakaliebiao.setVisibility(View.GONE);
                liebiaomoren.setNoResult();
            }
        }


    }

    @Override
    public void erroy() {
        pageXrefresh.stopRefresh();
        if (DakaxiangqingActivity.this != null)
            ToastUtil.show(DakaxiangqingActivity.this, "请求失败，请重试！");
    }

    @Override
    public void erry(String result) {
        pageXrefresh.stopRefresh();

        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(DakaxiangqingActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(DakaxiangqingActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    /**
     * 日期选择
     */
    public void getDate() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        final int[] date = {0, 0, 0};
        DateDialog birthDiolog = new DateDialog(this,
                new DateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String year, String month, String data) {
                        // TODO Auto-generated method stub
                        dates = (year + "-" + month + "-" + data);
                        titleTopbar.setText(dates);
                        initData();
                    }
                }, date[0], date[1], date[2], width, height);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = pageXrefresh.getLastRefreshTime();
        pageXrefresh.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        pagePresenter.dakatongjixiangqing(token, "20", s,dates, time);
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);
        pagePresenter.dakatongjixiangqing(token, "20", s,dates, time);
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
    private void showPopupMenu(LinearLayout btnQuestion) {
        mSpinerPopWindow.setWidth(btnQuestion.getWidth());
        mSpinerPopWindow.showAsDropDown(btnQuestion);
    }
    @Override
    public void onItemClick(int pos) {
        setHero(pos);
    }
    private void setHero(int postion) {
        if (postion >= 0 && postion <= nameList.size()) {
            String value = nameList.get(postion);
            shaixuanwenzi.setText(value);
            if(postion==0){
                isFocus="0";
            }else if (postion==1){
                isFocus="1";
            }
            page=1;
            String s = String.valueOf(page);
            pagePresenter.dakatongjixiangqing(token, "20", s,dates, time);
        }
    }
}
